# -*- coding: utf-8 -*-
import scrapy
from scrapy_quotes.items import ScrapyQuotesItem

class QuotesSpider(scrapy.Spider):
    name = 'quotes'
    allowed_domains = ['quotes.toscrape.com']
    start_urls = ['http://quotes.toscrape.com/']

    def parse(self, response):
        alldiv = response.xpath("//div[@class='quote']")
        for div in alldiv:
            item = ScrapyQuotesItem()
            item['text'] = div.xpath("./span[@class='text']/text()").extract()[0]
            item['author'] = div.xpath("./span/small[@class='author']/text()").extract()[0]
            item['tags'] = div.xpath("./div[@class='tags']/meta/@content").extract()[0]
            yield item

        try:
            nextlink = response.xpath("//li[@class='next']/a/@href").extract()[0]
            thenextlink = response.urljoin(nextlink)
            yield scrapy.Request(url=thenextlink, callback=self.parse)
        except:
            pass